package com.lmaos.basetools.sText;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.lmaos.basetools.base.SBaseUtils;
/**
 * 
 * @author lmaos
 *
 * <p>作者：张星宇</p>
 * <p>时间：2017年6月16日 下午1:54:59</p>
 * <p>说明:xml简单读取工具		</p>
 *
 */
public class XmlUtils {
	public static List<Document> getIncludeDocumentByClassPath(String filename,String includeTag,String arrtName){
		return getIncludeDocumentByClassPath(filename, XmlUtils.class.getClassLoader(), includeTag, arrtName);
	}
	public static List<Document> getIncludeDocumentByClassPath(String filename,ClassLoader classLoader,String includeTag,String arrtName){
		return getIncludeDocument(getDoucmentByClassPath(filename, classLoader), classLoader, includeTag, arrtName);
	
	}
	public static List<Document> getIncludeDocument(Document doc,String includeTag,String arrtName){
		return getIncludeDocument(doc, XmlUtils.class.getClassLoader(), includeTag, arrtName);
	}
	public static List<Document> getIncludeDocument(Document doc,ClassLoader classLoader,String includeTag,String arrtName){
		List<Document> docs = new ArrayList<Document>();
		if(doc!=null){
			docs.add(doc);
			for (int i = 0; i < docs.size(); i++) {
				NodeList includes = docs.get(i).getElementsByTagName(includeTag);
				for (int j = 0; j < includes.getLength(); j++) {
					Element include = getElement(includes, j);
					String xmlPath = getAttrOrContent(include, arrtName);
					Document includeDoc = getDoucmentByClassPath(xmlPath, classLoader);
					if(includeDoc != null){
						docs.add(includeDoc);
					}
				}
			}
		}
		return docs;
	}
	public static Document getDoucmentByClassPath(String fileName){
		return XmlUtils.getDoucmentByClassPath(fileName, XmlUtils.class.getClassLoader());
	}
	/**
	 * 说明：file:/path/file 来自一个具体路径,uri:http://path/file 来自一个连接, 不加前缀则获得当前应用 classPath下的文件		<br>
	 * 时间：2017年6月26日 上午9:23:21
	 *
	 * @param fileName	
	 * @param classLoader
	 * @return
	 */
	public static Document getDoucmentByClassPath(String fileName,ClassLoader classLoader){
		InputStream in = null;
		try {
			if(fileName.startsWith("file:")){
				fileName = fileName.substring("file:".length());
				in = new FileInputStream(fileName);
			}else if(fileName.startsWith("uri:")){
				fileName = fileName.substring("uri:".length());
				URI uri = new URI(fileName);
				in = uri.toURL().openStream();
			}else{
				in = classLoader.getResourceAsStream(fileName);
			}
			return getDoucment(in);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			SBaseUtils.close(in);
		}
	}
	public static Document getDoucment(InputStream in){
		try {
			return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			try {
				in.close();
			} catch (Exception e2) {
			}
		}
	}
	
	public static Element getElement(NodeList ns,int index){
		Node node = ns.item(index);
		if(node instanceof Element){
			return (Element) node;
		}
		return null;
	}
	
	public static Element indexElementByAttr(Element superNode,String attrName,String attrValue){
		return indexElementByAttr(superNode.getChildNodes(), attrName, attrValue);
	}
	public static Element indexElementByAttr(NodeList ns,String attrName,String attrValue){
		for (int i = 0; i < ns.getLength(); i++) {
			Element node = getElement(ns, i);
			if(node!=null&&attrValue.equals(node.getAttribute(attrName))){
				return node;
			}
		}
		return null;
	}
	/**
	 * 说明：查找属性为当前值当节点		<br>
	 * 时间：2017年6月16日 下午1:55:22
	 *
	 * @param ns
	 * @param attrNames
	 * @param attrValue
	 * @return
	 */
	public static Element indexElementByAttr(NodeList ns,String attrNames[],String attrValue){
		if(TextUtils.isEmpty(attrValue)){
			return null;
		}
		for (int i = 0; i < ns.getLength(); i++) {
			Element node = getElement(ns, i);
			for (String attrName : attrNames) {
				if(node!=null&&attrValue.equals(node.getAttribute(attrName))){
					return node;
				}
			}
		}
		return null;
	}
	public static String getAttr(Element node,String... attrNames){
		String attrValue = null;
		for (String attrName : attrNames) {
			attrValue = node.getAttribute(attrName);
			if(attrValue!=null&&!attrValue.isEmpty()){
				return attrValue;
			}
		}
		return attrValue;
	}
	public static String getAttrOrContent(Element node,String... attrNames){

		for (String attrName : attrNames) {
			String attrValue = node.getAttribute(attrName);
			if(attrValue!=null&&!attrValue.isEmpty()){
				return attrValue;
			}
		}
		return node.getTextContent();
	}
	public static void main(String[] args) {
		System.out.println(getDoucmentByClassPath("uri:http://localhost:8080/ERP_OA/a.xml", XmlUtils.class.getClassLoader()).getDocumentElement().getTagName());
	}
}
